package com.jxau.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * 数据库加密工具类
 */
public class DruidEncryptUtil {

    private static String publicKey;

    private static String privateKey;

    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey = keyPair[0];
            System.out.println("1:privateKey: " + privateKey);
            publicKey = keyPair[1];
            System.out.println("2:publicKey: " + publicKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }
    //加密
    public static String encrypt(String plainText) {
        try {
            String encrypt = ConfigTools.encrypt(privateKey, plainText);
            System.out.println("3:encrypt: " + encrypt);
            return encrypt;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    //解密
    public static String decrypt(String cipherText) {
        try {
            String decrypt = ConfigTools.decrypt(publicKey, cipherText);
            System.out.println("4:decrypt: " + decrypt);
            return decrypt;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        String plainText = "1234";
        String cipherText = encrypt(plainText);

    }
}
